<?php
class PublishVersionAction extends CommonAction {
	//版本列表，带分页和搜索
	public function index() {
		//判断是否有搜索条件
		if((isset($_REQUEST['version_type']) && (!empty($_REQUEST['version_type']))) 
			|| (isset($_REQUEST['version_name']) && (!empty($_REQUEST['version_name']))) 
			|| (isset($_REQUEST['start_time']) && (!empty($_REQUEST['start_time']))) 
			|| (isset($_REQUEST['end_time']) && (!empty($_REQUEST['end_time'])))) {
			$version_type = $_REQUEST['version_type'];
			$version_name = $_REQUEST['version_name'];
			$start_time	  = $_REQUEST['start_time'];
			$end_time 	  = $_REQUEST['end_time'];
			if($version_type != '0')  $data['version_type'] = $version_type;
			if(!empty($version_name)) $data['version_name'] = array('like','%'.$version_name.'%');
			if(!empty($start_time) && !empty($end_time)) {
				$data['publish_time'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)));
				$end_time = $end_time;
			} elseif(!empty($start_time)) {
				//如果开始时间输入，结束时间没输入，判断开始时间如果大于当前时间，则结束时间留空，否则结束时间=当前时间
				if(strtotime($start_time)<time()) {
					$end_time = time();
					$data['publish_time'] = array(array('EGT',strtotime($start_time)),array('ELT',$end_time));
					$end_time = date("Y-m-d",$end_time);
				} else {
					$data['publish_time'] = array('EGT',strtotime($start_time));
				}
			}
			$this->assign('version_type',$version_type);
			$this->assign('version_name',$version_name);
			$this->assign('start_time',$start_time);
			$this->assign('end_time',$end_time);
		}
		$count = M('update_version')->where($data)->count();
		import('ORG.Util.Page');
		$page = new Page($count,2);
		$list = M('update_version')->where($data)->limit($page->firstRow .','. $page->listRows)->select();
		//分页跳转带查询条件，采用request方式
		$page->parameter .= "&version_type=".$_REQUEST['version_type']."&version_name=".$_REQUEST['version_name']
						 ."&start_time=".$_REQUEST['start_time']."&end_time=".$_REQUEST['end_time'];
		$show = $page->show();
		$searchlist = M('update_version')->field('version_type')->distinct(true)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->assign('searchlist',$searchlist);
		$this->display();
	}
	
	//发布版本
	public function publishversion() {
		if ($_POST) {
			if (isset ( $_FILES ['filename'] ) && ! empty ( $_FILES ['filename'] ['name'] )) {
				import ( 'ORG.Net.UploadFile' );
				$uploadfile = new UploadFile ();
				$uploadfile->allowExts = array (
						'zip' 
				); // 设置附件上传类型
				$uploadfile->savePath = './update/';
				if (! $uploadfile->upload ()) { // 上传错误提示错误信息
					$this->error ( $uploadfile->getErrorMsg () );
				} else { // 上传成功 获取上传文件信息
					$info = $uploadfile->getUploadFileInfo ();
					$data ['filename'] = $info [0] ['name'];
					$data ['version_type'] = $_POST ['version_type'];
					$data ['version_name'] = $_POST ['version_name'];
					$data ['version_number'] = $_POST ['version_number'];
					$data ['history_versionnumber'] = $_POST ['history_versionnumber'];
					$data ['appname'] = $_POST ['appname'];
					$data ['pluginname'] = $_POST ['pluginname'];
					$data ['publish_time'] = time ();
					if (M ( 'update_version' )->order ( 'publish_time' )->add ( $data )) {
						$this->assign('jumpUrl',URL('admin/PublishVersion/index'));
						$this->success ( "发布成功" );
					} else {
						$this->error ( "发布失败" );
					}
				}
			}
		} else {
			$this->getAppAndPluginInfo();
			$this->display ();
		}
	}
	
	//获取应用和插件信息
	private function getAppAndPluginInfo() {
		$applist = M ( 'app' )->field ( 'app_name' )->select ();
		$pluginlist = M ( 'addons' )->field ( 'name' )->select ();
		$this->assign ( 'applist', $applist );
		$this->assign ( 'pluginlist', $pluginlist );
	}
	
	//进入修改页面
	public function update_publishversion() {
		if(isset($_GET['id'])) {
			$data['id'] = $_GET['id'];
			$find = M('update_version')->where($data)->find();
			$this->assign('find',$find);
			$this->getAppAndPluginInfo();
			$this->display('publishversion');
		}
	}
	
	//修改发布版本,修改的时候如果有上传信息的时候，则删除上次上传附件，否则附件仍为上次上传附件。
	public function doupdate_publishversion() {
		if ($_POST) {
			if (isset ( $_FILES ['filename'] ) && ! empty ( $_FILES ['filename'] ['name'] )) {
				import ( 'ORG.Net.UploadFile' );
				$uploadfile = new UploadFile ();
				$uploadfile->allowExts = array (
						'zip'
				); // 设置附件上传类型
				$uploadfile->savePath = './update/';
				if (! $uploadfile->upload ()) { // 上传错误提示错误信息
					$this->error ( $uploadfile->getErrorMsg () );
				} else { // 上传成功 获取上传文件信息
					$info = $uploadfile->getUploadFileInfo ();
					$data ['filename'] = $info [0] ['name'];
					unlink($info[0]['savepath'].$_POST['oldfilename']);
				}
			} else {
				$data['filename'] = $_POST['oldfilename'];
			}
			$data ['version_type'] = $_POST ['version_type'];
			$data ['version_name'] = $_POST ['version_name'];
			$data ['version_number'] = $_POST ['version_number'];
			$data ['history_versionnumber'] = $_POST ['history_versionnumber'];
			$data ['appname'] = $_POST ['appname'];
			$data ['pluginname'] = $_POST ['pluginname'];
			$data ['publish_time'] = time ();
			if (M ( 'update_version' )->where("id={$_POST['versionid']}")->order ( 'publish_time' )->save ( $data )) {
				$this->assign('jumpUrl',URL('admin/PublishVersion/index'));
				$this->success ( "修改成功" );
			} else {
				$this->error ( "修改失败" );
			}
		}
	}
	
	//删除发布版本
	public function doDeleteVersion() {
		$ids = $_POST['ids'];
		if ( empty($ids) ) {echo 0; return ;}
		//删除前先查询是否有附件，如果有附件，则删除数据后删除附件。
		$res = M('update_version')->where("id in(".$ids.")")->select();
		$result = M('update_version')->where("id in(".$ids.")")->delete();
		if($result) {
			foreach($res as $k => $v) {
				$filepath = './update/'.$v['filename'];
				if(file_exists($filepath)) {
					unlink($filepath);
				}
			}
			echo 1;
		} else {
			echo 0;
		}
	}
	
	//下载文件
	public function downloadFile() {
		if(isset($_GET['id'])) {
			$data['id'] = $_GET['id'];
			$find = M('update_version')->where($data)->find();
			$filepath = './update/'.$find['filename'];
			if(!empty($find['filename']) && file_exists($filepath)) {
				import('ORG.Net.Http');
				Http::download($filepath,$find['filename']);
			}
		}
	}
}
?>